<?php

namespace app\admin\model;

use think\Cache;
use think\Model;
use think\Log;
use app\common\library\User;

class ExpertUpgradeOrder extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected static function init()
    {
    }
    
    public function upLevel($user_id) {
        $user = db("user")->where(['id'=>$user_id])->find();
        if(empty($user)){
              return false;
        }
        //是否存在未完成的升级任务
            $count = db("expert_upgrade_order")->where("user_id",$user["id"])->whereIn("upgrade_type", [1, 2, 3])->whereIn("status", [0, 1, -1, -2])->count();
            // exit(db()->getLastSql());
            if ($count > 0) {
                return true;
            } else {
                //都完成了去升级
                try {
                     //处理用户的级别关系和占位
                     $row = User::upgradeExpert($user["id"], $user["expert_level"]);
                     if ($row) {
                         //插入升级记录
                         $history = [
                             "is_system" => $user["is_system_hand_upgrade"],
                             "admin_id" => $user["system_hand_upgrade_admin_id"],
                             "user_id" => $user["id"],
                             "before_level" => $user["before_expert_level"],
                             "after_level" => $user["upgradeing_expert_level"],
                             "note" => "定时器升级",
                             "addtime" => time()
                         ];
                         db("expert_upgrade_history")->insert($history);

                         //更新状态
                         db("user")->where("id", $user["id"])->update([
                             "is_system_hand_upgrade" => 0,
                             "system_hand_upgrade_admin_id" => 0,
                             "before_expert_level" => 0,
                             "upgradeing_expert_level" => 0,
                             "is_upgraded_expert_level" => 0,
                             "expert_level" => $user["upgradeing_expert_level"],
                         ]);
                         return true;
                     }else{
                         return false;
                     }

                 } catch (Exception $e) {
                     Log::write($e->getMessage());
                      return false;
                 }
                        
                return true;
            }
    }
    
    //隐藏功能检查
    public static function hideUser($userId) {
        //关闭隐藏功能
        
//        $user = \think\Db::name('user')->where(['id'=>$userId])->find();
//        if(!empty($user)){
//            if($user['admin_status']==0){ //开启应
//                $expire_times = 5;
//                if($user['expire_times']>=$expire_times){
//                    return   \think\Db::name('expert_relation')->where(['user_id'=>$userId])->data(["status"=>"0"])->update();
//                }
//            }
//            
//        }
        return false;
    }

}
